﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Web;
using System.Web.Mvc;
using CMSMVC4.Areas.Main.Models;
using CMSMVC4.Filters;

namespace CMSMVC4.Areas.Main.Controllers
{
    public class LienHeController : Controller
    {
        //
        // GET: /Main/LienHe/
        CMSMVC4Entities db = new CMSMVC4Entities();
        CommandSql cm = new CommandSql();

        [OutputCache(CacheProfile = "Cache15p")]
        public  ActionResult Index()
        {
            ViewBag.key = "Liên hệ tới Công Ty TNHH AnThi Việt Nam";
            ViewBag.description = "Công ty phân phối thiết bị và dịch vụ Anthi";
            ViewBag.Title = MultiResource.Resource.Title_Contact;
            //select menu dang chon
            var menu = db.Menus.FirstOrDefault(x => x.Type == MenuType.LienHe);
            if (menu != null)
            {
                ViewBag.Menu = menu.Id;
            }
            else
            {
                ViewBag.Menu = 0;
            }
            return View();
        }

        [ChildActionOnly]
        public ActionResult ListDiaChiLienHe()
        {
            var data = cm.GetConntact();
            return PartialView(data);
        }

        [ChildActionOnly]
        public ActionResult ListContact()
        {
            CMSMVC4Entities db = new CMSMVC4Entities();
            var data = db.Introductions.OrderBy(x=>x.Id).ToList();
            return PartialView(data);
        }

        [ChildActionOnly]
        public ActionResult FormLienHe()
        {
            FeedBackModel model = new FeedBackModel();
            return PartialView(model);
        }

        [HttpPost]
        public ActionResult FormLienHe(FeedBackModel model)
        {
            if (ModelState.IsValid)
            {
                Feedback row = new Feedback();
                row.Name = model.name;
                row.Email = model.email;
                row.Phone = model.phone;
                row.Message = model.data;
                row.Subject = "FeedBack";
                db.Feedbacks.AddObject(row);
                db.SaveChanges();
                ModelState.AddModelError("", MultiResource.Resource.Feedback_succsess);
                //----------send mail------------
                System.Net.Mail.MailMessage message = new System.Net.Mail.MailMessage();

                string mailto = System.Configuration.ConfigurationManager.AppSettings["EmailTo"];
                message.From = new System.Net.Mail.MailAddress("3dscanvietnam@gmail.com");
                message.To.Add(new System.Net.Mail.MailAddress(mailto));

                message.IsBodyHtml = true;
                message.BodyEncoding = Encoding.UTF8;
                message.Subject = "3DSCAN VIET NAM - FeedBack";
                message.Body = @"<span style='font-weight: bold;'>Người Gửi: </span><span style='color: rgb(173, 19, 19);font-size: 15px;'>" + model.name + @"</span> <br/>
                                 <span style='font-weight: bold;'>Email: </span><a href='mailto:" + model.email + "' target='_blank'>" + model.email + @"</a> <br/>
                                 <span style='font-weight: bold;'>Số Điện Thoại: </span><span style='color: rgb(173, 19, 19);font-size: 15px;'>" + model.phone + @"</span><br/>
                                 <span style='font-weight: bold;'>Nội Dung Tin Nhắn: </span><span style='color: rgb(173, 19, 19);font-size: 15px;'>" + model.data + @"</span><p/>
                                 <span style='font-weight: bold;'>Link Quản Lý: </span><a href='http://3dscan.com.vn/Admin/Feedback/ListFeedback' target='_blank'>http://3dscan.com.vn/Admin/Feedback/ListFeedback</a>";

                System.Net.Mail.SmtpClient client = new System.Net.Mail.SmtpClient();
                client.Timeout = 15000;
                client.Send(message);
                //-------------------------------
                model = new FeedBackModel();
            }
            if (Request.IsAjaxRequest())
            {
                return PartialView(model);
            }
            return RedirectToAction("Index","LienHe");
        }

    }
}
